﻿from django.http import HttpResponse
from forum.models import theme,discussion,topic,confirm,captcha
#from django.template.loader import get_template
#from django.template.loader import Context
from django.template import RequestContext
from django.shortcuts import *
from django.core.context_processors import csrf
from forum.models import theme
from django.contrib import auth
from django.contrib.auth.models import User
import random
import sendmail
from datetime import datetime
def login(request):
	if 'username' and 'password' in request.POST:
		
		username = request.POST['username']
		password = request.POST['password']
		user = auth.authenticate(username=username, password=password)
		if user is not None and user.is_active:
        
			auth.login(request, user)
        
			return HttpResponseRedirect("/")
		else: 
			return HttpResponseRedirect("/accounts/login/")
       
	else : 
		return render_to_response('login.html', context_instance=RequestContext(request))
	
		

def logout(request):
    auth.logout(request)
    return HttpResponseRedirect("/")

def index(request):
	authuser = request.user.username
	alltheme=theme.objects.all()
	alldiscussion=discussion.objects.order_by("-rating")
	
	return render_to_response('index.html', {'user':authuser,'alltheme':alltheme,'alldiscussion':alldiscussion}, context_instance=RequestContext(request))
def adddiscussion(request,offset):
	if 'title' and 'message' in request.POST:
		discussion(title=request.POST['title'],text=request.POST['message'],themeid=offset).save()
		return HttpResponseRedirect("/")
	else:
		alltheme=theme.objects.all()
		return render_to_response('adddiscussion.html', {'alltheme':alltheme,'curdiscussion':offset}, context_instance=RequestContext(request))

def showtopic(request,offset):
	authuser = request.user.username
	allpost=topic.objects.filter(discussionid=offset)
	alltheme=theme.objects.all()
	firstpost=discussion.objects.all()[int(offset)-1].text
	return render_to_response('showmessages.html', {'fpost':firstpost,'user':authuser,'alltheme':alltheme,'allpost':allpost,'discussionid':offset}, context_instance=RequestContext(request))

def addtopic(request,offset):

	if 'message' in request.POST:
		d = datetime.today()
		topic(text=request.POST['message'],autor='admin',cdata=d.date(),discussionid=offset).save()
		
		return HttpResponseRedirect("/discussion/%s/" % offset)

def addtheme(request):
	if 'title' in request.POST:
		theme(title=request.POST['title']).save()
		return HttpResponseRedirect("/")
	else:
		alltheme=theme.objects.all()
		return render_to_response('addtheme.html', {'alltheme':alltheme}, context_instance=RequestContext(request))

def show_discussion(request,offset):
	authuser = request.user.username
	alltheme=theme.objects.all()
	alldiscussion=discussion.objects.filter(themeid=offset)
	return render_to_response('showdiscussion.html', {'user':authuser,'alltheme':alltheme,'alldiscussion':alldiscussion,'curdiscussion':offset}, context_instance=RequestContext(request))

def registration(request):
	if 'username' and 'password' and 'rpassword' and 'mail' and 'captcha' in request.POST:
		
		user = request.POST['username']
		pas = request.POST['password']
		rpas = request.POST['rpassword']
		email=request.POST['mail']
		if pas==rpas and request.POST['captcha']==capt[0].value :
			user = User.objects.create_user(username=request.POST['username'],email=request.POST['mail'],password=request.POST['password'])
			user.is_active = False
			user.save()
			#user = auth.authenticate(username=username, password=password)
			#auth.login(request, user)
			rand=random.randint(1000, 10000)
			
			confirm(username=user,password=pas,userid=rand).save()
			
			text = 'Здравствуйте !\n Подтвердите регистрацию \n http://127.0.0.1:8000/accounts/registration/%s/' %rand
			sendmail.sendconfirm (email,'Подтверждение регистрации',text)

			return HttpResponseRedirect("/")
		else :
			caprand=random.randint(1, 4)
			global capt
			#capt=captcha.objects.filter(id=caprand)
			capt=captcha.objects.filter(id=caprand)
			#
			return render_to_response('registration.html',{'capname':capt[0].filename}, context_instance=RequestContext(request))
	else:
	
		caprand=random.randint(1, 4)
		global capt
		#capt=captcha.objects.filter(id=caprand)
		capt=captcha.objects.filter(id=caprand)
		#
		return render_to_response('registration.html',{'capname':capt[0].filename}, context_instance=RequestContext(request))
def confirmregistration(request,offset):
	conf=confirm.objects.filter(userid=offset)
	user = auth.authenticate(username=conf[0].username, password=conf[0].password)
	user.is_active = True
	auth.login(request, user)
	return HttpResponseRedirect("/")